package com.le.edu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.le.edu.domain.dto.TeacherQuery;
import com.le.edu.domain.entity.Teacher;
import com.le.edu.domain.vo.TeacherAndCourseListVO;
import com.le.edu.domain.vo.TeacherVO;
import com.le.edu.page.TableDataInfo;
import com.le.edu.resp.Result;
import com.le.edu.service.TeacherService;
import com.le.edu.utils.PasswordEncoder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author Le
 * @Create 2024-01-07 20:09
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "教师接口")
@RequestMapping("/teacher")
public class TeacherController extends BaseController {
    private final TeacherService teacherService;

    @Operation(summary = "根据id获取教师信息和课程信息")
    @GetMapping("/{teacherId}")
    public Result<TeacherAndCourseListVO> getById(@PathVariable("teacherId") Long teacherId) {
        return Result.ok(teacherService.getTeacherAndCourse(teacherId));
    }

    @Operation(summary = "查询所有教师信息")
    @GetMapping("/allData")
    public TableDataInfo<Teacher> allData() {
        return TableDataInfo.build(teacherService.list(new LambdaQueryWrapper<Teacher>()
                .orderByDesc(Teacher::getCreateTime)));
    }

    @Operation(summary = "查询所有教师信息-只有name和id")
    @GetMapping("/all")
    public Result<List<Teacher>> listNameAndId() {
        return Result.ok(teacherService.listNameAndId());
    }

    @Operation(summary = "分页查询教师信息")
    @PostMapping("/page")
    public TableDataInfo<TeacherVO> pageTeacherCondition(@RequestBody TeacherQuery dto) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getDelFlag, "0")
                .like(StrUtil.isNotBlank(dto.getName()), Teacher::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getLevel()), Teacher::getLevel, dto.getLevel())
                .between(Objects.nonNull(dto.getBeginTime()) && Objects.nonNull(dto.getEndTime()),
                        Teacher::getCreateTime, dto.getBeginTime(), dto.getEndTime())
                .orderByDesc(Teacher::getCreateTime);
        Page<TeacherVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        teacherService.pageTeacherList(page, wrapper);
        return TableDataInfo.build(page);
    }

    @Operation(summary = "批量删除教师")
    @DeleteMapping("/{teacherIds}")
    public Result<Void> remove(@PathVariable("teacherIds") Long[] teacherIds) {
        return toAjax(teacherService.removeByIds(Arrays.asList(teacherIds)));
    }

    @Operation(summary = "重置密码")
    @PutMapping("/resetPassword")
    public Result<Void> resetPassword(@RequestBody Teacher teacher) {
        return toAjax(teacherService.resetPassword(teacher));
    }

    @Operation(summary = "修改教师")
    @PutMapping
    public Result<Void> updateUser(@RequestBody Teacher teacher) {
        teacherService.checkUsernameUniqueNeTeacherId(teacher.getTeacherId(), teacher.getName());
        String rowPassword = teacher.getPassword();
        if (StrUtil.isNotBlank(rowPassword)) {
            teacher.setPassword(PasswordEncoder.encrypt(rowPassword));
        }
        return toAjax(teacherService.updateById(teacher));
    }

    @Operation(summary = "新增教师")
    @PostMapping
    public Result<Void> addUser(@RequestBody Teacher teacher) {
        teacherService.checkUsernameUnique(teacher.getName());
        teacher.setPassword(PasswordEncoder.encrypt(teacher.getPassword()));
        return toAjax(teacherService.save(teacher));
    }
}
